package com.xiay.stringboot.controller;


import com.alibaba.fastjson.JSONObject;
import com.xiay.stringboot.doman.Employee;
import com.xiay.stringboot.doman.Result;
import com.xiay.stringboot.repository.EmployeeRepository;
import com.xiay.stringboot.server.EmployeeService;
import com.xiay.stringboot.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by H-209-FX-XY on 2018/4/23.
 */
@RestController//当于@ResponseBody ＋ @Controller合在一起的作用。 无法返回jsp页面
public class EmployeeController {


    @Autowired
    EmployeeService employeeService;

    @PostMapping(value = "/saveEmployee")
    public void saveEmployee(Employee employee){
        employeeService.save(employee);
    }

    @GetMapping(value = "/findAllEmployee")
    public Result findAllEmployee(){
        System.out.println("findAllEmployee()");
        List<Employee> list = employeeService.findAll();
        return ResultUtil.success(list);
    }

    @PostMapping(value = "/findByUsername/{username}")
    public Result findByUsername(@PathVariable String  username){
        List<Employee> list = employeeService.findByUsername(username);
        return ResultUtil.success(list);
    }

    @PostMapping(value = "/findById/{id}")
    public Result findById(@PathVariable Integer id) throws Exception {
        return employeeService.findById(id);
    }

}
